package com.cssw.fyzb.busi.api.user;

import com.cssw.fyzb.core.model.dodb.User;
import com.cssw.fyzb.core.model.vo.user.RegisterUserInfo;
import com.cssw.fyzb.busi.service.user.UserService;
import com.cssw.fyzb.busi.util.CodeUtil;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import com.ydn.simplecache.SimpleCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.cssw.fyzb.busi.cache.CacheKeys.forRegister;
import static com.cssw.fyzb.busi.util.Bizs.ERROR_FORMAT_LOGIN_PWD;

/**
 * 新增用户
 *
 * @author Feng Chen
 */
@Function(description = "用户注册无密码", parameters = {
        @Parameter(name = "mobile", type = Type.String, description = "手机号码", required = true),
        @Parameter(name = "loginPwd", type = Type.String, description = "登录密码", required = true)
})
@Component
public class RegisterAddLoginPwd implements Action {

    @Autowired
    private UserService userService;

    @Autowired
    private SimpleCache simpleCache;

    private static final Logger logger = LoggerFactory.getLogger(Register.class);

    @Override
    public Response execute(Request request) throws Exception {
        String mobile = request.getString("mobile");
        String loginPwd = request.getString("loginPwd");

        try {
            RegisterUserInfo registerUserInfo = simpleCache.get(forRegister(mobile));
            System.out.println(registerUserInfo);

            if (loginPwd == null || loginPwd.length() < 6 || loginPwd.length() > 18) {
                return Response.fail(ERROR_FORMAT_LOGIN_PWD.message, ERROR_FORMAT_LOGIN_PWD.code);
            }

            if (!judgeContainsStr(loginPwd)){
                return Response.fail("登录密码必须包含字母");
            }

            if (registerUserInfo == null) {
                logger.error("用户缓存不存在或者已失效");
                return Response.fail("用户缓存不存在或者已失效");
            }

            String invitCode = registerUserInfo.getInvitCode();
            Long userId = CodeUtil.decode(invitCode);
            User supUser = userService.getById(userId);
            if (null == supUser){
                return Response.fail("邀请码不正确");
            }
            //判断是否实名
            if (!User.FACE_AUTH_YES.equals(supUser.getFaceAuth())) {
                return Response.fail("推荐人未实名");
            }
            // 保存用户
            userService.save(userId, mobile, loginPwd, null);
            Map data = new HashMap();
            data.put("userId", userId);
            return Response.success().put("data", data);
        } catch (Exception e) {
            logger.error("", e);
            return Response.fail("新增用户失败");
        }
    }

    public boolean judgeContainsStr(String name) {
        String regex=".*[a-zA-Z]+.*";
        Matcher m= Pattern.compile(regex).matcher(name);
        return m.matches();
    }

}
